<?php
namespace addon\wechat_config;

use app\common\controller\AddonBase;
use app\common\dao\AddonInterface;
use tpfcore\Core;
use think\Db;

class WechatConfig extends AddonBase implements AddonInterface
{
    private $addonName='微信公众号管理插件';
    /**
     * 插件安装
     */
    public function addonInstall()
    {
        try{
            $this->addonInstallBefore();
            return [RESULT_SUCCESS, $this->addonName.'安装成功'];
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        } 
    }
    
    /**
     * 插件卸载
     */
    public function addonUninstall()
    {   
        try{
            $this->addonUninstallBefore();
            return [RESULT_SUCCESS, $this->addonName.'卸载成功'];
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }   
    }
    
    /**
     * 插件基本信息
     */
    public function addonInfo()
    {
        return [
            'name' => 'wechat_config',
            'title' => '微信公众号管理', 
            'describe' => '微信公众号管理插件', 
            'author' => 'xiaojun', 
            'version' => '2.0' ,
            'require'=>'>=3.0', 
            'type'=>'module',
            'config'=>false
        ];
    }
    
    /**
     * 插件配置信息
     */
    public function addonConfig()
    {
        return [];
    }
    /**
     * 插件安装前的一些操作
     */
    public function addonInstallBefore(){
        // 安装前要进行的一些sql处理
        try{
            
            $result = Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='微信管理'");
            
            if(empty($result)){
                Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('微信管理', 'payment', '', '', '1', '', '1', '', 'comments ', '4', '$parentid');");
                $parentid=Db::getLastInsID();
            }else{
                $parentid=$result[0]['id'];
            }
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('微信公众号管理', 'wechat_config', 'WechatConfig', 'index', '1', '', '1', '', 'wechat', '0', '$parentid');");

            $parentid=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('公众号管理', 'wechat_config', 'WechatConfig', 'index', '1', '', '1', '', 'th', '0', '$parentid');");

        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }  
    }
    /**
     * 卸载前的一些操作
     */
    public function addonUninstallBefore(){

        try{

            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='微信公众号管理'");

            if(!empty($result)){

                $parentid=$result[0]['id'];

                Db::execute("DELETE FROM `".DB_PREFIX."menu` WHERE id=$parentid OR parentid=$parentid");

            }
        }catch(\Exception $e){

            throw new \Exception("sql 语句执行错误".$e->getMessage());

        }
    }
}
